package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.query.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private IPermissionService permissionService;

    //由于dataGrid组件返回必须为json,所以跳转界面就分开
    @RequestMapping("view")
    public String view(){
        return "permission";
    }

    //页面数据
    @RequestMapping("query")
    @ResponseBody
    public List<Permission> query(){
        return permissionService.selectAll();
    }
    //角色编辑回显
    @RequestMapping("rolePermisson")
    @ResponseBody
    public List<Permission> selectByRoleId(Long roleId){
        if (roleId != null) {
            return permissionService.selectByRoleId(roleId);
        }
        return null;
    }

    //权限加载
    @RequestMapping("reload")
    @ResponseBody
    public JSONResult reload(){
        JSONResult result = new JSONResult();
        try{
            permissionService.reload();
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

}
